package system

import (
	"errors"
	"fmt"
	"gitcode.com/G-YT/ANN/server/model/catering"
	"gitcode.com/G-YT/ANN/server/model/disease"
	"gitcode.com/G-YT/ANN/server/model/manager"
	systemRes "gitcode.com/G-YT/ANN/server/model/system/response"
	"gitcode.com/G-YT/ANN/server/model/user"
	"gitcode.com/G-YT/ANN/server/plugin/geo/model"
	"strconv"
	"strings"
	"time"

	"gitcode.com/G-YT/ANN/server/model/common"
	systemReq "gitcode.com/G-YT/ANN/server/model/system/request"

	"gitcode.com/G-YT/ANN/server/global"
	"gitcode.com/G-YT/ANN/server/model/system"
	"gitcode.com/G-YT/ANN/server/utils"
	"github.com/google/uuid"
	"gorm.io/gorm"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: Register
//@description: 用户注册
//@param: u model.SysUser
//@return: userInter system.SysUser, err error

type UserService struct{}

var UserServiceApp = new(UserService)

func (userService *UserService) Register(u system.SysUser) (userInter system.SysUser, err error) {
	var user system.SysUser
	if !errors.Is(global.GVA_DB.Where("username = ?", u.Username).First(&user).Error, gorm.ErrRecordNotFound) { // 判断用户名是否注册
		return userInter, errors.New("用戶名或手機號已註冊")
	}
	if !errors.Is(global.GVA_DB.Where("name = ?", u.Name).First(&user).Error, gorm.ErrRecordNotFound) { // 判断用户名是否注册
		return userInter, errors.New("名稱已存在")
	}
	// 否则 附加uuid 密码hash加密 注册
	u.Password = utils.BcryptHash(u.Password)
	u.UUID = uuid.New()
	err = global.GVA_DB.Create(&u).Error
	return u, err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@author: [SliverHorn](https://github.com/SliverHorn)
//@function: Login
//@description: 用户登录
//@param: u *model.SysUser
//@return: err error, userInter *model.SysUser

func (userService *UserService) Login(u *system.SysUser) (userInter *system.SysUser, err error) {
	if nil == global.GVA_DB {
		return nil, fmt.Errorf("db not init")
	}

	var user system.SysUser
	err = global.GVA_DB.Where("username = ?", u.Username).Preload("Authorities").Preload("Authority").First(&user).Error
	if err == nil {
		if ok := utils.BcryptCheck(u.Password, user.Password); !ok {
			return nil, errors.New("密碼錯誤")
		}
		MenuServiceApp.UserAuthorityDefaultRouter(&user)
	}
	return &user, err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: ChangePassword
//@description: 修改用户密码
//@param: u *model.SysUser, newPassword string
//@return: err error

func (userService *UserService) ChangePassword(u *system.SysUser, newPassword string) (err error) {
	var user system.SysUser
	err = global.GVA_DB.Select("id, password").Where("id = ?", u.ID).First(&user).Error
	if err != nil {
		return err
	}
	if ok := utils.BcryptCheck(u.Password, user.Password); !ok {
		return errors.New("原密碼錯誤")
	}
	pwd := utils.BcryptHash(newPassword)
	err = global.GVA_DB.Model(&user).Update("password", pwd).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetUserInfoList
//@description: 分页获取数据
//@param: info request.PageInfo
//@return: err error, list interface{}, total int64

func (userService *UserService) GetUserInfoList(info systemReq.GetUserList) (list interface{}, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	db := global.GVA_DB.Model(&system.SysUser{})
	var userList []system.SysUser

	if info.NickName != "" {
		db = db.Where("nick_name LIKE ?", "%"+info.NickName+"%")
	}
	if info.Phone != "" {
		db = db.Where("phone LIKE ?", "%"+info.Phone+"%")
	}
	if info.Username != "" {
		db = db.Where("username LIKE ?", "%"+info.Username+"%")
	}
	if info.Email != "" {
		db = db.Where("email LIKE ?", "%"+info.Email+"%")
	}

	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Limit(limit).Offset(offset).Preload("Authorities").Preload("Authority").Find(&userList).Error
	return userList, total, err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: SetUserAuthority
//@description: 设置一个用户的权限
//@param: uuid uuid.UUID, authorityId string
//@return: err error

func (userService *UserService) SetUserAuthority(id uint, authorityId uint) (err error) {

	assignErr := global.GVA_DB.Where("sys_user_id = ? AND sys_authority_authority_id = ?", id, authorityId).First(&system.SysUserAuthority{}).Error
	if errors.Is(assignErr, gorm.ErrRecordNotFound) {
		return errors.New("該用戶無此角色")
	}

	var authority system.SysAuthority
	err = global.GVA_DB.Where("authority_id = ?", authorityId).First(&authority).Error
	if err != nil {
		return err
	}
	var authorityMenu []system.SysAuthorityMenu
	var authorityMenuIDs []string
	err = global.GVA_DB.Where("sys_authority_authority_id = ?", authorityId).Find(&authorityMenu).Error
	if err != nil {
		return err
	}

	for i := range authorityMenu {
		authorityMenuIDs = append(authorityMenuIDs, authorityMenu[i].MenuId)
	}

	var authorityMenus []system.SysBaseMenu
	err = global.GVA_DB.Preload("Parameters").Where("id in (?)", authorityMenuIDs).Find(&authorityMenus).Error
	if err != nil {
		return err
	}
	hasMenu := false
	for i := range authorityMenus {
		if authorityMenus[i].Name == authority.DefaultRouter {
			hasMenu = true
			break
		}
	}
	if !hasMenu {
		return errors.New("找不到預設路由，無法切換本角色")
	}

	err = global.GVA_DB.Model(&system.SysUser{}).Where("id = ?", id).Update("authority_id", authorityId).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: SetUserAuthorities
//@description: 设置一个用户的权限
//@param: id uint, authorityIds []string
//@return: err error

func (userService *UserService) SetUserAuthorities(adminAuthorityID, id uint, authorityIds []uint) (err error) {
	return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		var user system.SysUser
		TxErr := tx.Where("id = ?", id).First(&user).Error
		if TxErr != nil {
			global.GVA_LOG.Debug(TxErr.Error())
			return errors.New("查詢用戶數據失敗")
		}
		TxErr = tx.Delete(&[]system.SysUserAuthority{}, "sys_user_id = ?", id).Error
		if TxErr != nil {
			return TxErr
		}
		var useAuthority []system.SysUserAuthority
		for _, v := range authorityIds {
			e := AuthorityServiceApp.CheckAuthorityIDAuth(adminAuthorityID, v)
			if e != nil {
				return e
			}
			useAuthority = append(useAuthority, system.SysUserAuthority{
				SysUserId: id, SysAuthorityAuthorityId: v,
			})
		}
		TxErr = tx.Create(&useAuthority).Error
		if TxErr != nil {
			return TxErr
		}
		TxErr = tx.Model(&user).Update("authority_id", authorityIds[0]).Error
		if TxErr != nil {
			return TxErr
		}
		// 返回 nil 提交事务
		return nil
	})
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteUser
//@description: 删除用户
//@param: id float64
//@return: err error

func (userService *UserService) DeleteUser(id int) (err error) {
	return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Where("id = ?", id).Delete(&system.SysUser{}).Error; err != nil {
			return err
		}
		if err := tx.Delete(&[]system.SysUserAuthority{}, "sys_user_id = ?", id).Error; err != nil {
			return err
		}
		return nil
	})
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: SetUserInfo
//@description: 设置用户信息
//@param: reqUser model.SysUser
//@return: err error, user model.SysUser

func (userService *UserService) SetUserInfo(req system.SysUser) error {
	selectFields := []string{"updated_at", "name", "nick_name", "header_img", "phone", "email", "side_mode", "enable", "province", "city", "area", "addr", "open_num"}
	updates := map[string]interface{}{
		"updated_at": time.Now(),
		"name":       req.Name,
		"nick_name":  req.NickName,
		"header_img": req.HeaderImg,
		"phone":      req.Phone,
		"email":      req.Email,
		"enable":     req.Enable,
		"province":   req.Province,
		"city":       req.City,
		"area":       req.Area,
		"addr":       req.Addr,
		"open_num":   req.OpenNum,
	}
	// 仅当密码非空时更新密码（假设不允许空密码）
	if req.Password != "" {
		selectFields = append(selectFields, "password")
		updates["password"] = utils.BcryptHash(req.Password)
	}
	return global.GVA_DB.Model(&system.SysUser{}).Select(selectFields).Where("id = ?", req.ID).Updates(updates).Error
}

// SetUserEnable 设置用户状态
func (userService *UserService) SetUserEnable(req system.SysUser) error {
	return global.GVA_DB.Model(&system.SysUser{}).
		Select("updated_at", "name", "nick_name", "header_img", "phone", "email", "sideMode", "enable", "province", "city", "area", "addr", "open_num").
		Where("id=?", req.ID).
		Update("enable", req.Enable).Error
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: SetSelfInfo
//@description: 设置用户信息
//@param: reqUser model.SysUser
//@return: err error, user model.SysUser

func (userService *UserService) SetSelfInfo(req system.SysUser) error {
	return global.GVA_DB.Model(&system.SysUser{}).
		Where("id=?", req.ID).
		Updates(req).Error
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: SetSelfSetting
//@description: 设置用户配置
//@param: req datatypes.JSON, uid uint
//@return: err error

func (userService *UserService) SetSelfSetting(req common.JSONMap, uid uint) error {
	return global.GVA_DB.Model(&system.SysUser{}).Where("id = ?", uid).Update("origin_setting", req).Error
}

//@author: [piexlmax](https://github.com/piexlmax)
//@author: [SliverHorn](https://github.com/SliverHorn)
//@function: GetUserInfo
//@description: 获取用户信息
//@param: uuid uuid.UUID
//@return: err error, user system.SysUser

func (userService *UserService) GetUserInfo(uuid uuid.UUID) (user system.SysUser, err error) {
	var reqUser system.SysUser
	err = global.GVA_DB.Preload("Authorities").Preload("Authority").First(&reqUser, "uuid = ?", uuid).Error
	if err != nil {
		return reqUser, err
	}
	MenuServiceApp.UserAuthorityDefaultRouter(&reqUser)
	return reqUser, err
}

//@author: [SliverHorn](https://github.com/SliverHorn)
//@function: FindUserById
//@description: 通过id获取用户信息
//@param: id int
//@return: err error, user *model.SysUser

func (userService *UserService) FindUserById(id int) (user *system.SysUser, err error) {
	var u system.SysUser
	err = global.GVA_DB.Where("id = ?", id).First(&u).Error
	return &u, err
}

//@author: [SliverHorn](https://github.com/SliverHorn)
//@function: FindUserByUuid
//@description: 通过uuid获取用户信息
//@param: uuid string
//@return: err error, user *model.SysUser

func (userService *UserService) FindUserByUuid(uuid string) (user *system.SysUser, err error) {
	var u system.SysUser
	if err = global.GVA_DB.Where("uuid = ?", uuid).First(&u).Error; err != nil {
		return &u, errors.New("用户不存在")
	}
	return &u, nil
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: ResetPassword
//@description: 修改用户密码
//@param: ID uint
//@return: err error

func (userService *UserService) ResetPassword(ID uint, password string) (err error) {
	err = global.GVA_DB.Model(&system.SysUser{}).Where("id = ?", ID).Update("password", utils.BcryptHash(password)).Error
	return err
}

// GetServiceCenterMapData // 获取服务中心地图数据
// Author: [g-qs](https://gitee.com/g-qs)
func (userService *UserService) GetServiceCenterMapData() (list []systemRes.CountNum, err error) {
	var services []system.SysUser
	var cities []model.GeoCity
	var healthManagers []manager.FdHealthManager
	// 查询所有的服务中心
	// Select("(SELECT name FROM geo_cities WHERE code = `sys_users`.city) AS name,id,city").
	if servicesErr := global.GVA_DB.Select("(SELECT name FROM geo_cities WHERE code = `sys_users`.city) AS name,id,city").Where("authority_id = ?", 333).Find(&services).Error; servicesErr != nil {
		return list, servicesErr
	}
	// 查询城市信息
	if citiesErr := global.GVA_DB.Where("code IN ? ", getSysUserCityIDs(services)).Find(&cities).Error; citiesErr != nil {
		return list, citiesErr
	}
	// 构建城市经纬度映射
	geoCoordMap := make(map[string][2]float64)
	for _, city := range cities {
		geoCoordMap[city.Code] = [2]float64{city.Longitude, city.Latitude}
	}
	// 统计每个服务中心下的健管师数量
	for _, serviceCenter := range services {
		global.GVA_DB.Where("store_id IN (SELECT id FROM sys_users WHERE creator = ?)", serviceCenter.ID).Find(&healthManagers)
		// 健管师数量统计
		list = append(list, systemRes.CountNum{
			Name:      serviceCenter.Name,
			Value:     uint(len(healthManagers)),
			LonAndLat: geoCoordMap[serviceCenter.City],
		})
	}
	//list = []systemRes.CountNum{
	//	{
	//		Name:  "长沙市",
	//		Value: 4,
	//		LonAndLat: [2]float64{
	//			112.94547319535287,
	//			28.23488939994364,
	//		},
	//	},
	//	{
	//		Name:  "市辖区(上海)",
	//		Value: 1,
	//		LonAndLat: [2]float64{
	//			121.48053886017651,
	//			31.235929042252014,
	//		},
	//	},
	//	{
	//		Name:  "贵阳市",
	//		Value: 1,
	//		LonAndLat: [2]float64{
	//			106.63657676352776,
	//			26.653324822309752,
	//		},
	//	},
	//	{
	//		Name:  "青岛市",
	//		Value: 7,
	//		LonAndLat: [2]float64{
	//			120.38945519114627,
	//			36.072227496663224,
	//		},
	//	},
	//	{
	//		Name:  "海口市",
	//		Value: 6,
	//		LonAndLat: [2]float64{
	//			110.32552547126409,
	//			20.04404943925674,
	//		},
	//	},
	//	{
	//		Name:  "长春市",
	//		Value: 6,
	//		LonAndLat: [2]float64{
	//			125.3306020759069,
	//			43.82195350104314,
	//		},
	//	},
	//	{
	//		Name:  "南京市",
	//		Value: 6,
	//		LonAndLat: [2]float64{
	//			118.80242172124585,
	//			32.06465288561847,
	//		},
	//	},
	//	{
	//		Name:  "市辖区(重庆)",
	//		Value: 1,
	//		LonAndLat: [2]float64{
	//			106.55843415537664,
	//			29.568996245338923,
	//		},
	//	},
	//	{
	//		Name:  "杭州市",
	//		Value: 20,
	//		LonAndLat: [2]float64{
	//			120.21551180372168,
	//			30.25308298169347,
	//		},
	//	},
	//	{
	//		Name:  "驻马店市",
	//		Value: 7,
	//		LonAndLat: [2]float64{
	//			114.02847078173271,
	//			33.01784241674367,
	//		},
	//	},
	//	{
	//		Name:  "合肥市",
	//		Value: 0,
	//		LonAndLat: [2]float64{
	//			117.23344266497664,
	//			31.826577833686887,
	//		},
	//	},
	//	{
	//		Name:  "淄博市",
	//		Value: 3,
	//		LonAndLat: [2]float64{
	//			118.06145253489896,
	//			36.81908568332188,
	//		},
	//	},
	//	{
	//		Name:  "市辖区(天津)",
	//		Value: 1,
	//		LonAndLat: [2]float64{
	//			117.2095232146708,
	//			39.093667843403956,
	//		},
	//	},
	//	{
	//		Name:  "哈尔滨市",
	//		Value: 1,
	//		LonAndLat: [2]float64{
	//			126.54161509031663,
	//			45.808825827952184,
	//		},
	//	},
	//	{
	//		Name:  "市辖区(北京)",
	//		Value: 19,
	//		LonAndLat: [2]float64{
	//			116.4133836971231,
	//			39.910924547299565,
	//		},
	//	},
	//	{
	//		Name:  "呼和浩特市",
	//		Value: 4,
	//		LonAndLat: [2]float64{
	//			111.75550856170946,
	//			40.84842299711348,
	//		},
	//	},
	//	{
	//		Name:  "杭州市",
	//		Value: 18,
	//		LonAndLat: [2]float64{
	//			120.21551180372168,
	//			30.25308298169347,
	//		},
	//	},
	//	{
	//		Name:  "成都市",
	//		Value: 0,
	//		LonAndLat: [2]float64{
	//			104.08153351042463,
	//			30.655821878416408,
	//		},
	//	},
	//	{
	//		Name:  "温州市",
	//		Value: 0,
	//		LonAndLat: [2]float64{
	//			120.70647689035565,
	//			28.00108540447221,
	//		},
	//	},
	//	{
	//		Name:  "桂林市",
	//		Value: 0,
	//		LonAndLat: [2]float64{
	//			110.20354537457943,
	//			25.242885724872647,
	//		},
	//	},
	//}
	return
}

// GetStationsMapDataByHm // 获取下属服务站地图数据【服务中心】
// Author: [g-qs](https://gitee.com/g-qs)
func (userService *UserService) GetStationsMapDataByHm(CentreID uint) (info systemRes.StationMapData, err error) {
	var stations []system.SysUser
	var cities []model.GeoCity
	var provinces model.GeoProvince
	var healthManagers []manager.FdHealthManager
	// 查询所有的服务站
	if servicesErr := global.GVA_DB.Select("(SELECT name FROM geo_cities WHERE code = `sys_users`.city) AS name,id,province,city").Where("authority_id = 444 AND creator = ?", CentreID).Find(&stations).Error; servicesErr != nil {
		return info, servicesErr
	}
	var countNums []systemRes.CountNum
	// 查询城市信息
	if citiesErr := global.GVA_DB.Where("code IN ? ", getSysUserCityIDs(stations)).Find(&cities).Error; citiesErr != nil {
		return info, citiesErr
	}
	// 查询省份信息
	if provincesErr := global.GVA_DB.Where("code IN ? ", getSysUserProvinceIDs(stations)).First(&provinces).Error; provincesErr != nil {
		return info, provincesErr
	}
	// 构建城市经纬度映射
	geoCoordMap := make(map[string][2]float64)
	for _, city := range cities {
		geoCoordMap[city.Code] = [2]float64{city.Longitude, city.Latitude}
	}
	// 统计每个服务中心下的健管师数量
	for _, station := range stations {
		global.GVA_DB.Where("store_id = ?", station.ID).Find(&healthManagers)
		// 健管师数量统计
		countNums = append(countNums, systemRes.CountNum{
			Name:      station.Name,
			Value:     uint(len(healthManagers)),
			LonAndLat: geoCoordMap[station.City],
		})
	}
	centre := [2]float64{}
	var provinceName string
	centre = [2]float64{provinces.Longitude, provinces.Latitude}
	provinceName = provinces.Name
	// 构造最终返回的数据
	info = systemRes.StationMapData{
		CountNums: countNums,
		Centre:    centre,
		Province:  provinceName,
	}
	return
}

// GetStationsMapDataByUser // 获取下属服务站地图数据【服务中心】
// Author: [g-qs](https://gitee.com/g-qs)
func (userService *UserService) GetStationsMapDataByUser(CentreID uint) (info systemRes.StationMapData, err error) {
	var stations []system.SysUser
	var cities []model.GeoCity
	var provinces model.GeoProvince
	var users []user.User
	// 查询所有的服务站
	if servicesErr := global.GVA_DB.Select("(SELECT name FROM geo_cities WHERE code = `sys_users`.city) AS name,id,province,city").Where("authority_id = 444 AND creator = ?", CentreID).Find(&stations).Error; servicesErr != nil {
		return info, servicesErr
	}
	var countNums []systemRes.CountNum
	// 查询城市信息
	if citiesErr := global.GVA_DB.Where("code IN ? ", getSysUserCityIDs(stations)).Find(&cities).Error; citiesErr != nil {
		return info, citiesErr
	}
	// 查询城市信息
	if provincesErr := global.GVA_DB.Where("code IN ? ", getSysUserProvinceIDs(stations)).First(&provinces).Error; provincesErr != nil {
		return info, provincesErr
	}
	// 构建城市经纬度映射
	geoCoordMap := make(map[string][2]float64)
	for _, city := range cities {
		geoCoordMap[city.Code] = [2]float64{city.Longitude, city.Latitude}
	}
	// 统计每个服务站下的用户数量
	for _, station := range stations {
		global.GVA_DB.Where("service_center_id = ?", station.ID).Find(&users)
		// 健管师数量统计
		countNums = append(countNums, systemRes.CountNum{
			Name:      station.Name,
			Value:     uint(len(users)),
			LonAndLat: geoCoordMap[station.City],
		})
	}
	centre := [2]float64{}
	var provinceName string
	centre = [2]float64{provinces.Longitude, provinces.Latitude}
	provinceName = provinces.Name
	// 构造最终返回的数据
	info = systemRes.StationMapData{
		CountNums: countNums,
		Centre:    centre,
		Province:  provinceName,
	}
	return
}

// GetServiceStationMapData // 获取服务站地图数据
// Author: [g-qs](https://gitee.com/g-qs)
func (userService *UserService) GetServiceStationMapData() (list []systemRes.CountNum, err error) {
	var stations []system.SysUser
	var cities []model.GeoCity
	var users []user.User
	// 查询所有的服务站
	if stationsErr := global.GVA_DB.Select("(SELECT name FROM geo_cities WHERE code = `sys_users`.city) AS name,id,city").Where("authority_id = ?", 444).Find(&stations).Error; stationsErr != nil {
		return list, stationsErr
	}
	// 查询城市信息
	if citiesErr := global.GVA_DB.Where("code IN ? ", getSysUserCityIDs(stations)).Find(&cities).Error; citiesErr != nil {
		return list, citiesErr
	}
	// 构建城市经纬度映射
	geoCoordMap := make(map[string][2]float64)
	for _, city := range cities {
		geoCoordMap[city.Code] = [2]float64{city.Longitude, city.Latitude}
	}
	// 统计每个服务中心下的用户数
	for _, v := range stations {
		global.GVA_DB.Where("service_center_id IN (?)", v.ID).Find(&users)
		// 健管师数量统计
		list = append(list, systemRes.CountNum{
			Name:      v.Name,
			Value:     uint(len(users)),
			LonAndLat: geoCoordMap[v.City],
		})
	}
	//list = []systemRes.CountNum{
	//	{
	//		Name:  "海口市",
	//		Value: 31,
	//		LonAndLat: [2]float64{
	//			110.32552547126409,
	//			20.04404943925674,
	//		},
	//	},
	//	{
	//		Name:  "长沙市",
	//		Value: 9,
	//		LonAndLat: [2]float64{
	//			112.94547319535287,
	//			28.23488939994364,
	//		},
	//	},
	//	{
	//		Name:  "市辖区(上海)",
	//		Value: 4,
	//		LonAndLat: [2]float64{
	//			121.48053886017651,
	//			31.235929042252014,
	//		},
	//	},
	//	{
	//		Name:  "贵阳市",
	//		Value: 1,
	//		LonAndLat: [2]float64{
	//			106.63657676352776,
	//			26.653324822309752,
	//		},
	//	},
	//	{
	//		Name:  "青岛市",
	//		Value: 177,
	//		LonAndLat: [2]float64{
	//			120.38945519114627,
	//			36.072227496663224,
	//		},
	//	},
	//	{
	//		Name:  "长春市",
	//		Value: 2,
	//		LonAndLat: [2]float64{
	//			125.3306020759069,
	//			43.82195350104314,
	//		},
	//	},
	//	{
	//		Name:  "长春市",
	//		Value: 380,
	//		LonAndLat: [2]float64{
	//			125.3306020759069,
	//			43.82195350104314,
	//		},
	//	},
	//	{
	//		Name:  "南京市",
	//		Value: 303,
	//		LonAndLat: [2]float64{
	//			118.80242172124585,
	//			32.06465288561847,
	//		},
	//	},
	//	{
	//		Name:  "市辖区(重庆)",
	//		Value: 78,
	//		LonAndLat: [2]float64{
	//			106.55843415537664,
	//			29.568996245338923,
	//		},
	//	},
	//	{
	//		Name:  "杭州市",
	//		Value: 153,
	//		LonAndLat: [2]float64{
	//			120.21551180372168,
	//			30.25308298169347,
	//		},
	//	},
	//	{
	//		Name:  "驻马店市",
	//		Value: 32,
	//		LonAndLat: [2]float64{
	//			114.02847078173271,
	//			33.01784241674367,
	//		},
	//	},
	//	{
	//		Name:  "蚌埠市",
	//		Value: 0,
	//		LonAndLat: [2]float64{
	//			117.39551332813694,
	//			32.921523704350825,
	//		},
	//	},
	//	{
	//		Name:  "淄博市",
	//		Value: 10,
	//		LonAndLat: [2]float64{
	//			118.06145253489896,
	//			36.81908568332188,
	//		},
	//	},
	//	{
	//		Name:  "淄博市",
	//		Value: 13,
	//		LonAndLat: [2]float64{
	//			118.06145253489896,
	//			36.81908568332188,
	//		},
	//	},
	//	{
	//		Name:  "市辖区(天津)",
	//		Value: 9,
	//		LonAndLat: [2]float64{
	//			117.2095232146708,
	//			39.093667843403956,
	//		},
	//	},
	//	{
	//		Name:  "佳木斯市",
	//		Value: 12,
	//		LonAndLat: [2]float64{
	//			130.327359092573,
	//			46.80568999085779,
	//		},
	//	},
	//	{
	//		Name:  "哈尔滨市",
	//		Value: 0,
	//		LonAndLat: [2]float64{
	//			126.54161509031663,
	//			45.808825827952184,
	//		},
	//	},
	//	{
	//		Name:  "青岛市",
	//		Value: 5,
	//		LonAndLat: [2]float64{
	//			120.38945519114627,
	//			36.072227496663224,
	//		},
	//	},
	//	{
	//		Name:  "市辖区(北京)",
	//		Value: 63,
	//		LonAndLat: [2]float64{
	//			116.4133836971231,
	//			39.910924547299565,
	//		},
	//	},
	//	{
	//		Name:  "呼和浩特市",
	//		Value: 7,
	//		LonAndLat: [2]float64{
	//			111.75550856170946,
	//			40.84842299711348,
	//		},
	//	},
	//	{
	//		Name:  "市辖区(重庆)",
	//		Value: 0,
	//		LonAndLat: [2]float64{
	//			106.55843415537664,
	//			29.568996245338923,
	//		},
	//	},
	//	{
	//		Name:  "绍兴市",
	//		Value: 1,
	//		LonAndLat: [2]float64{
	//			120.58547847885335,
	//			30.0363693113069,
	//		},
	//	},
	//	{
	//		Name:  "绍兴市",
	//		Value: 0,
	//		LonAndLat: [2]float64{
	//			120.58547847885335,
	//			30.0363693113069,
	//		},
	//	},
	//	{
	//		Name:  "绍兴市",
	//		Value: 0,
	//		LonAndLat: [2]float64{
	//			120.58547847885335,
	//			30.0363693113069,
	//		},
	//	},
	//	{
	//		Name:  "绍兴市",
	//		Value: 2,
	//		LonAndLat: [2]float64{
	//			120.58547847885335,
	//			30.0363693113069,
	//		},
	//	},
	//	{
	//		Name:  "绍兴市",
	//		Value: 4,
	//		LonAndLat: [2]float64{
	//			120.58547847885335,
	//			30.0363693113069,
	//		},
	//	},
	//	{
	//		Name:  "杭州市",
	//		Value: 61,
	//		LonAndLat: [2]float64{
	//			120.21551180372168,
	//			30.25308298169347,
	//		},
	//	},
	//	{
	//		Name:  "绍兴市",
	//		Value: 2,
	//		LonAndLat: [2]float64{
	//			120.58547847885335,
	//			30.0363693113069,
	//		},
	//	},
	//	{
	//		Name:  "杭州市",
	//		Value: 0,
	//		LonAndLat: [2]float64{
	//			120.21551180372168,
	//			30.25308298169347,
	//		},
	//	},
	//	{
	//		Name:  "成都市",
	//		Value: 0,
	//		LonAndLat: [2]float64{
	//			104.08153351042463,
	//			30.655821878416408,
	//		},
	//	},
	//	{
	//		Name:  "温州市",
	//		Value: 0,
	//		LonAndLat: [2]float64{
	//			120.70647689035565,
	//			28.00108540447221,
	//		},
	//	},
	//	{
	//		Name:  "桂林市",
	//		Value: 0,
	//		LonAndLat: [2]float64{
	//			110.20354537457943,
	//			25.242885724872647,
	//		},
	//	},
	//	{
	//		Name:  "南京市",
	//		Value: 0,
	//		LonAndLat: [2]float64{
	//			118.80242172124585,
	//			32.06465288561847,
	//		},
	//	},
	//	{
	//		Name:  "吉林市",
	//		Value: 0,
	//		LonAndLat: [2]float64{
	//			126.55563450495482,
	//			43.84356783457924,
	//		},
	//	},
	//	{
	//		Name:  "临沂市",
	//		Value: 0,
	//		LonAndLat: [2]float64{
	//			118.36353300501388,
	//			35.11067124236514,
	//		},
	//	},
	//	{
	//		Name:  "绍兴市",
	//		Value: 0,
	//		LonAndLat: [2]float64{
	//			120.58547847885335,
	//			30.0363693113069,
	//		},
	//	},
	//}
	return
}

// GetServiceStationRank // 获取服务中心下服务站数量排名
// Author: [g-qs](https://gitee.com/g-qs)
func (userService *UserService) GetServiceStationRank() (list []systemRes.StatisticalResults, err error) {
	// 查询每个服务中心下的健管中心数量
	err = global.GVA_DB.Model(&system.SysUser{}).
		Select("`sys_users`.name, COUNT(b.id) AS value, GROUP_CONCAT(b.id) AS IDs").
		Joins("LEFT JOIN sys_users AS b ON b.creator = `sys_users`.id").
		Where("b.authority_id = 444 AND `sys_users`.authority_id = 333").
		Group("`sys_users`.id").
		Order("value DESC").
		Scan(&list).Error
	//list = []systemRes.StatisticalResults{
	//	{
	//		Name:  "绍兴服务中心",
	//		Value: 8,
	//		IDs:   "54,55,56,40,57,63,74,53",
	//	},
	//	{
	//		Name:  "青岛服务中心",
	//		Value: 3,
	//		IDs:   "48,73,23",
	//	},
	//	{
	//		Name:  "吉林省服务中心",
	//		Value: 2,
	//		IDs:   "28,72",
	//	},
	//	{
	//		Name:  "南京服务中心",
	//		Value: 2,
	//		IDs:   "30,71",
	//	},
	//	{
	//		Name:  "重庆服务中心",
	//		Value: 2,
	//		IDs:   "39,52",
	//	},
	//	{
	//		Name:  "淄博服务中心",
	//		Value: 2,
	//		IDs:   "43,44",
	//	},
	//	{
	//		Name:  "黑龙江服务中心",
	//		Value: 2,
	//		IDs:   "46,47",
	//	},
	//	{
	//		Name:  "杭州服务中心",
	//		Value: 2,
	//		IDs:   "60,66",
	//	},
	//	{
	//		Name:  "长沙服务中心",
	//		Value: 1,
	//		IDs:   "20",
	//	},
	//	{
	//		Name:  "上海服务中心",
	//		Value: 1,
	//		IDs:   "21",
	//	},
	//	{
	//		Name:  "贵州服务中心",
	//		Value: 1,
	//		IDs:   "22",
	//	},
	//	{
	//		Name:  "海南服务中心",
	//		Value: 1,
	//		IDs:   "19",
	//	},
	//	{
	//		Name:  "驻马店服务中心",
	//		Value: 1,
	//		IDs:   "41",
	//	},
	//	{
	//		Name:  "安徽省服务中心",
	//		Value: 1,
	//		IDs:   "42",
	//	},
	//	{
	//		Name:  "天津服务中心",
	//		Value: 1,
	//		IDs:   "45",
	//	},
	//	{
	//		Name:  "北京服务中心",
	//		Value: 1,
	//		IDs:   "49",
	//	},
	//	{
	//		Name:  "内蒙服务中心",
	//		Value: 1,
	//		IDs:   "51",
	//	},
	//	{
	//		Name:  "成都服务中心",
	//		Value: 1,
	//		IDs:   "67",
	//	},
	//	{
	//		Name:  "温州服务中心",
	//		Value: 1,
	//		IDs:   "68",
	//	},
	//	{
	//		Name:  "桂林服务中心",
	//		Value: 1,
	//		IDs:   "70",
	//	},
	//}
	return
}

// GetHealthManagerRank // 获取服务中心下健管师数量排名
// Author: [g-qs](https://gitee.com/g-qs)
func (userService *UserService) GetHealthManagerRank() (list []systemRes.StatisticalResults, err error) {
	err = global.GVA_DB.Model(&system.SysUser{}).
		Select("`sys_users`.name, COUNT(c.hm_id) AS value, GROUP_CONCAT(c.hm_id) AS IDs").
		Joins("LEFT JOIN sys_users AS b ON b.creator = `sys_users`.id").
		Joins("LEFT JOIN fd_health_manager AS c ON c.store_id = b.id").
		Where("b.authority_id = 444 AND `sys_users`.authority_id = 333").
		Group("`sys_users`.id").
		Order("value DESC").
		Scan(&list).Error
	//list = []systemRes.StatisticalResults{
	//	{
	//		Name:  "绍兴服务中心",
	//		Value: 20,
	//		IDs:   "39,45,46,51,57,52,53,58,54,76,56,60,59,64,61,55,72,94,95,103",
	//	},
	//	{
	//		Name:  "北京服务中心",
	//		Value: 19,
	//		IDs:   "91,92,93,97,98,99,101,102,104,107,108,109,38,110,71,111,73,113,90",
	//	},
	//	{
	//		Name:  "杭州服务中心",
	//		Value: 18,
	//		IDs:   "77,78,79,80,82,83,84,85,86,87,88,68,89,69,112,70,74,75",
	//	},
	//	{
	//		Name:  "青岛服务中心",
	//		Value: 7,
	//		IDs:   "23,42,43,44,27,31,32",
	//	},
	//	{
	//		Name:  "驻马店服务中心",
	//		Value: 7,
	//		IDs:   "47,65,19,20,22,24,26",
	//	},
	//	{
	//		Name:  "海南服务中心",
	//		Value: 6,
	//		IDs:   "6,8,29,33,37,40",
	//	},
	//	{
	//		Name:  "吉林省服务中心",
	//		Value: 6,
	//		IDs:   "1,100,7,17,67,81",
	//	},
	//	{
	//		Name:  "南京服务中心",
	//		Value: 6,
	//		IDs:   "105,106,9,18,50,96",
	//	},
	//	{
	//		Name:  "长沙服务中心",
	//		Value: 4,
	//		IDs:   "4,30,62,63",
	//	},
	//	{
	//		Name:  "内蒙服务中心",
	//		Value: 4,
	//		IDs:   "48,49,114,41",
	//	},
	//	{
	//		Name:  "淄博服务中心",
	//		Value: 3,
	//		IDs:   "34,35,36",
	//	},
	//	{
	//		Name:  "上海服务中心",
	//		Value: 1,
	//		IDs:   "16",
	//	},
	//	{
	//		Name:  "贵州服务中心",
	//		Value: 1,
	//		IDs:   "66",
	//	},
	//	{
	//		Name:  "重庆服务中心",
	//		Value: 1,
	//		IDs:   "21",
	//	},
	//	{
	//		Name:  "天津服务中心",
	//		Value: 1,
	//		IDs:   "25",
	//	},
	//	{
	//		Name:  "黑龙江服务中心",
	//		Value: 1,
	//		IDs:   "28",
	//	},
	//	{
	//		Name:  "安徽省服务中心",
	//		Value: 0,
	//		IDs:   "",
	//	},
	//	{
	//		Name:  "成都服务中心",
	//		Value: 0,
	//		IDs:   "",
	//	},
	//	{
	//		Name:  "温州服务中心",
	//		Value: 0,
	//		IDs:   "",
	//	},
	//	{
	//		Name:  "桂林服务中心",
	//		Value: 0,
	//		IDs:   "",
	//	},
	//}
	return
}

// GetStationsRankByHmNum 获取服务站排名根据健管师数量【服务中心】
// Author: [g-qs](https://gitee.com/g-qs)
func (userService *UserService) GetStationsRankByHmNum(CentreID uint) (list []systemRes.StatisticalResults, err error) {
	err = global.GVA_DB.Model(&system.SysUser{}).
		Select("`sys_users`.`name`, COUNT(b.hm_id) AS `value`, GROUP_CONCAT(b.hm_id) AS IDs").
		Joins("LEFT JOIN fd_health_manager AS b ON b.`store_id` = `sys_users`.`id`").
		Where("`sys_users`.`authority_id` = 444 AND `sys_users`.`creator` = ?", CentreID).
		Group("`sys_users`.id").
		Order("`value` DESC").
		Scan(&list).Error
	return
}

// GetUserRank // 获取服务中心下用户数量排名
// Author: [g-qs](https://gitee.com/g-qs)
func (userService *UserService) GetUserRank() (list []systemRes.StatisticalResults, err error) {
	err = global.GVA_DB.Model(&system.SysUser{}).
		Select("`sys_users`.name, COUNT(c.id) AS value, GROUP_CONCAT(c.id) AS IDs").
		Joins("LEFT JOIN sys_users AS b ON b.creator = `sys_users`.id").
		Joins("LEFT JOIN fd_user AS c ON c.service_center_id = b.id").
		Where("b.authority_id = 444 AND `sys_users`.authority_id = 333").
		Group("`sys_users`.id").
		Order("value DESC").
		Scan(&list).Error
	//list = []systemRes.StatisticalResults{
	//	{
	//		Name:  "吉林省服务中心",
	//		Value: 380,
	//		IDs:   "8737,7776,8352,8216,8972,8791,7879,8366,8226,8985,8839,8043,8383,8242,8995,8857,8129,8401,8250,9024,8890,8139,8416,8262,9043,8147,8919,8431,8275,9071,8161,8949,8465,8284,9094,8174,8510,8301,9106,8182,8607,8314,9119,8192,8692,8342,8202,8739,7777,8359,8219,8975,8799,7922,8368,8227,8986,8843,8055,8384,8243,8996,8864,8130,8402,8252,9025,8897,8140,8418,8265,9044,8148,8920,8436,8276,9075,8164,8950,8466,8286,9096,8175,8527,8303,9107,8183,8608,8315,9120,8193,8704,8343,8206,8742,7778,8360,8220,8976,8805,7934,8372,8231,8987,8844,8070,8385,8244,9003,8866,8131,8404,8253,9029,8898,8141,8419,8266,9046,8149,8922,8440,8277,9079,8165,8955,8468,8287,9098,8176,8536,8304,9110,8184,8612,8319,9124,8195,8711,8344,8209,8210,8747,7781,8361,8221,8978,8813,7941,8373,8233,8988,8846,8093,8386,8245,9008,8867,8133,8406,8254,9031,8899,8142,8420,8267,9051,8150,8925,8441,8279,9081,8166,8956,8475,8289,9099,8177,8542,8307,9112,8185,8614,8320,9125,8197,8712,8345,8211,8756,7782,8362,8222,8980,8829,7970,8376,8234,8989,8847,8113,8387,8246,9009,8877,8134,8412,8255,9033,8902,8143,8421,8269,9056,8152,8928,8442,8280,9084,8167,8958,8476,8291,9100,8178,8555,8308,9113,8186,8643,8325,9126,8198,8713,8347,13,8349,8213,8968,8761,7790,8363,8223,8981,8830,7989,8377,8236,8991,8849,8126,8389,8247,9014,8878,8135,8413,8256,9038,8910,8144,8423,8270,9057,8155,8931,8446,8281,9085,8171,8962,8481,8294,9101,8179,8589,8309,9114,8187,8663,8333,9127,8199,8719,28,8350,8214,8969,8770,7815,8364,8224,8982,8833,8021,8378,8237,8993,8854,8127,8392,8248,9015,8885,8137,8414,8257,9039,8911,8145,8425,8273,9059,8158,8946,8449,8282,9087,8172,8967,8483,8295,9103,8180,8591,8312,9115,8188,8687,8339,9128,8200,8726,8729,29,8351,8215,8970,8772,7858,8365,8225,8983,8835,8041,8380,8238,8994,8855,8128,8399,8249,9017,8889,8138,8415,8261,9041,8146,8917,8428,8274,9064,8160,8948,8463,8283,9093,8173,8489,8299,9105,8181,8595,8313,9117,8191,8689,8340,8201",
	//	},
	//	{
	//		Name:  "南京服务中心",
	//		Value: 303,
	//		IDs:   "8490,8091,8896,8001,8575,8101,8908,8010,8664,8109,8924,8018,8121,8957,8027,8163,8979,8035,8239,9022,8045,8290,9042,8053,8758,8324,9067,8065,8798,8381,9083,8074,8871,8426,9104,8083,8884,7993,8493,8092,8900,8002,8601,8102,8913,8011,8675,8110,8932,8019,8125,8959,8028,8168,8984,8036,8240,9026,8046,8292,9045,8057,8762,8327,9070,8066,8806,8382,9086,8075,8873,8443,9116,8084,8886,7994,8498,8094,8901,8003,8611,8103,8914,8012,8678,8111,8934,8020,8136,8960,8029,8169,8998,8037,8241,9027,8047,8302,9047,8058,8773,8328,9072,8067,8807,8390,9088,8076,8874,8451,9118,8085,8887,7995,8501,8095,8903,8005,8619,8104,8915,8013,8725,8114,8935,8022,8153,8963,8030,8194,8999,8038,8251,9030,8048,8310,9049,8059,8774,8331,9073,8068,8834,8394,9089,8078,8876,8456,9121,8086,8888,7996,8891,7997,8504,8097,8904,8006,8646,8105,8916,8014,8731,8115,8937,8023,8154,8964,8031,8196,9002,8039,8258,9034,8049,8311,9050,8060,8777,8336,9076,8069,8840,8398,9090,8079,8879,8467,9122,8087,8893,7998,8512,8098,8905,8007,8652,8106,8918,8015,8116,8938,8024,8156,8973,8032,8204,9007,8040,8260,9036,8050,8734,8318,9053,8061,8780,8370,9078,8071,8868,8405,9095,8080,8880,8470,9123,8088,8089,8894,7999,8535,8099,8906,8008,8653,8107,8921,8016,8119,8942,8025,8157,8974,8033,8207,9016,8042,8271,9037,8051,8735,8322,9055,8062,8782,8375,9080,8072,8869,8407,9097,8081,8881,8472,8090,8895,8000,8549,8100,8907,8009,8662,8108,8923,8017,8120,8954,8026,8162,8977,8034,8235,9019,8044,8288,9040,8052,8746,8323,9060,8063,8794,8379,9082,8073,8870,8411,9102,8082,8882,7780,8486",
	//	},
	//	{
	//		Name:  "青岛服务中心",
	//		Value: 182,
	//		IDs:   "7991,7881,8732,8132,7889,8232,7897,8332,7905,8388,7913,8525,7928,8706,7944,8810,7956,8997,7969,7827,9054,7982,7866,7867,8004,7882,8733,8170,7890,8259,7898,8334,7906,8393,7914,8576,7930,8710,7945,8812,7957,9004,7973,7842,9077,7983,7868,8064,7883,8189,7891,8264,7899,8353,7907,8397,7915,8642,7931,8722,7946,8821,7958,9006,7974,7845,9091,7984,7985,7869,8077,7884,8203,7892,8278,7900,8354,7908,8424,7916,8647,7932,8728,7947,8824,7959,9010,7975,7854,9092,7986,7871,8112,7885,8217,7893,8285,7901,8356,7909,8432,7917,8661,7933,8792,7948,8875,7960,7793,9011,7976,7861,9111,7987,7873,7792,8117,7886,8218,7894,8300,7902,8357,7910,8433,7919,8667,7938,8793,7950,8927,7961,7800,9012,7977,7863,7988,7874,7794,8118,7887,8228,7895,8306,7903,8358,7911,8437,7926,8684,7939,8804,7951,8990,7965,7810,9028,7979,7864,7990,7877,7803,8124,7888,8230,7896,8316,7904,8371,7912,8477,7927,8685,7940,8809,7954,8992,7967,7825,9032,7981,7865",
	//	},
	//	{
	//		Name:  "绍兴服务中心",
	//		Value: 162,
	//		IDs:   "7952,8787,8341,8594,8571,8478,8583,8505,8596,8515,8610,8523,8623,8546,8634,8557,8650,8565,8668,8714,7953,8329,8788,8367,8648,8572,8480,8584,8506,8597,8516,8613,8524,8624,8547,8635,8558,8651,8566,8693,8715,7955,8811,8417,8573,8484,8585,8507,8598,8517,8615,8530,8626,8548,8636,8559,8654,8567,8694,8717,8293,8817,8427,8574,8801,8485,8586,8508,8600,8518,8616,8531,8627,8550,8637,8560,8655,8568,8697,8718,8738,8305,8326,8845,8429,8578,8816,8487,8587,8509,8602,8519,8617,8537,8628,8551,8640,8561,8658,8569,8698,7925,8748,8335,8669,8848,8430,8579,8495,8588,8511,8603,8520,8620,8538,8629,8552,8641,8562,8659,8699,7935,8765,8337,8348,8852,8434,8580,8500,8592,8513,8604,8521,8621,8539,8630,8554,8644,8563,8660,8700,7936,8766,8338,8553,8570,8471,8581,8503,8593,8514,8609,8522,8622,8540,8631,8556,8649,8564,8665,8708,7949",
	//	},
	//	{
	//		Name:  "重庆服务中心",
	//		Value: 78,
	//		IDs:   "7798,7820,7833,7844,7924,8263,8461,8590,8940,8953,7799,7822,7834,7862,7937,8268,8469,8632,8941,8966,7801,7823,7837,7870,7964,8391,8496,8638,8943,9108,7804,7828,7838,7876,7971,8403,8502,8673,8944,9109,7806,7829,7839,7880,7972,8410,8528,8827,8945,7807,7830,7840,7920,7980,8454,8529,8892,8947,7795,7814,7831,7841,7921,8205,8459,8532,8929,8951,7796,7816,7832,7843,7923,8212,8460,8533,8939,8952",
	//	},
	//	{
	//		Name:  "北京服务中心",
	//		Value: 63,
	//		IDs:   "8670,8784,8819,8837,8860,8936,9052,9074,8683,8785,8820,8838,8861,9000,9058,7836,8690,8786,8822,8842,8862,9001,9061,8447,8695,8802,8823,8850,8863,9005,9062,8452,8701,8803,8825,8851,8865,9013,9063,8464,8705,8814,8826,8853,8909,9018,9065,8491,8707,8815,8828,8856,8926,9023,9066,8666,8779,8818,8836,8858,8930,9035,9068",
	//	},
	//	{
	//		Name:  "杭州服务中心",
	//		Value: 61,
	//		IDs:   "8771,8795,8912,8544,8677,8727,8751,8775,8796,9048,8545,8679,8730,8752,8776,8797,8473,9069,8605,8680,8736,8753,8778,8808,8474,8606,8681,8703,8740,8754,8781,8831,8479,8639,8682,8716,8741,8757,8783,8832,8499,8656,8688,8720,8745,8760,8789,8841,8526,8671,8702,8721,8749,8763,8790,8859,8541,8672,8724,8750,8768",
	//	},
	//	{
	//		Name:  "驻马店服务中心",
	//		Value: 32,
	//		IDs:   "7787,7850,8298,8618,7789,7857,8317,8645,7808,7878,8330,8676,7813,7962,8395,8686,7817,7963,8396,8691,7818,7966,8408,9020,7824,8296,8422,9021,7786,7826,8297,8455",
	//	},
	//	{
	//		Name:  "海南服务中心",
	//		Value: 31,
	//		IDs:   "7802,8445,8599,8764,7819,8448,8674,8769,7846,8457,8709,8800,7872,8462,8723,8883,8159,8494,8743,8965,8208,8534,8744,8971,8439,8543,8755,7779,8444,8577,8759",
	//	},
	//	{
	//		Name:  "淄博服务中心",
	//		Value: 23,
	//		IDs:   "7809,8458,7852,7918,8482,7853,7929,7788,7855,7968,7835,7856,7978,7847,7859,8151,7848,7860,8450,7849,8346,8453,7851",
	//	},
	//	{
	//		Name:  "黑龙江服务中心",
	//		Value: 12,
	//		IDs:   "8229,7791,8272,7811,8374,7875,8633,8054,8056,8096,8122,8123",
	//	},
	//	{
	//		Name:  "长沙服务中心",
	//		Value: 9,
	//		IDs:   "8190,8355,8488,8492,8582,8696,7821,8767,7992",
	//	},
	//	{
	//		Name:  "天津服务中心",
	//		Value: 9,
	//		IDs:   "7812,8369,8400,8497,8625,8657,8933,7805,8961",
	//	},
	//	{
	//		Name:  "内蒙服务中心",
	//		Value: 7,
	//		IDs:   "7797,7942,7943,8321,8409,8435,8872",
	//	},
	//	{
	//		Name:  "上海服务中心",
	//		Value: 4,
	//		IDs:   "3,11,33,2",
	//	},
	//	{
	//		Name:  "贵州服务中心",
	//		Value: 1,
	//		IDs:   "8438",
	//	},
	//	{
	//		Name:  "安徽省服务中心",
	//		Value: 0,
	//		IDs:   "",
	//	},
	//	{
	//		Name:  "成都服务中心",
	//		Value: 0,
	//		IDs:   "",
	//	},
	//	{
	//		Name:  "温州服务中心",
	//		Value: 0,
	//		IDs:   "",
	//	},
	//	{
	//		Name:  "桂林服务中心",
	//		Value: 0,
	//		IDs:   "",
	//	},
	//}
	return
}

// GetServiceCenterDiseaseList // 获取服务中心主病症列表
// Author: [g-qs](https://gitee.com/g-qs)
func (userService *UserService) GetServiceCenterDiseaseList() (list []systemRes.MainDiseaseList, err error) {
	var services []system.SysUser
	var stations []system.SysUser
	var users []user.User
	var records []disease.DiseaseReportRecord
	// 查询所有的服务中心
	if servicesErr := global.GVA_DB.Where("authority_id = ?", 333).Find(&services).Error; servicesErr != nil {
		return list, servicesErr
	}
	// 统计每个服务中心下的用户数量
	for _, serviceCenter := range services {
		// 查询与当前服务中心关联的所有服务站
		global.GVA_DB.Where("creator = ?", serviceCenter.ID).Find(&stations)
		var userIDs []uint
		for _, stationValue := range stations {
			// 查询与当前服务站关联的所有用户
			global.GVA_DB.Table("fd_user USE INDEX (service_center_id)").Where("service_center_id = ?", stationValue.ID).Find(&users)
			for _, userValue := range users {
				userIDs = append(userIDs, userValue.ID)
			}
		}
		// 查询与当前用户关联的所有病症报告记录
		global.GVA_DB.Table("fd_disease_report_record USE INDEX (uid)").
			Joins("INNER JOIN ( SELECT uid, MAX( created_at ) AS latest_date FROM fd_disease_report_record WHERE uid IN (?) GROUP BY uid ) latest_records ON fd_disease_report_record.uid = latest_records.uid", userIDs).
			Where("fd_disease_report_record.created_at = latest_records.latest_date").Find(&records)
		// 统计疾病出现次数
		diseaseCounts := map[string]int{}
		for _, record := range records {
			diseases := strings.Split(record.BzIDs, ",")
			for _, diseaseID := range diseases {
				diseaseCounts[diseaseID]++
			}
		}
		// 找出出现次数最多的疾病
		maxCount := 0
		mainDiseaseID := ""
		for diseaseID, count := range diseaseCounts {
			if count > maxCount {
				maxCount = count
				mainDiseaseID = diseaseID
			}
		}
		// 查询主要疾病名称
		var bz disease.Disease
		if bzErr := global.GVA_DB.First(&bz, "id = ?", mainDiseaseID).Error; bzErr != nil {
			continue
		}
		list = append(list, systemRes.MainDiseaseList{
			Name:    serviceCenter.Name,
			Disease: bz.Name,
		})
	}
	//list = []systemRes.MainDiseaseList{
	//	{
	//		Name:    "长沙服务中心",
	//		Disease: "咽部不适",
	//	},
	//	{
	//		Name:    "上海服务中心",
	//		Disease: "腰膝冷痛",
	//	},
	//	{
	//		Name:    "青岛服务中心",
	//		Disease: "眼花",
	//	},
	//	{
	//		Name:    "海南服务中心",
	//		Disease: "胃寒",
	//	},
	//	{
	//		Name:    "吉林省服务中心",
	//		Disease: "失眠",
	//	},
	//	{
	//		Name:    "南京服务中心",
	//		Disease: "视力减退",
	//	},
	//	{
	//		Name:    "重庆服务中心",
	//		Disease: "健忘",
	//	},
	//	{
	//		Name:    "绍兴服务中心",
	//		Disease: "眼花",
	//	},
	//	{
	//		Name:    "驻马店服务中心",
	//		Disease: "尿黄",
	//	},
	//	{
	//		Name:    "淄博服务中心",
	//		Disease: "健忘",
	//	},
	//	{
	//		Name:    "天津服务中心",
	//		Disease: "高血压",
	//	},
	//	{
	//		Name:    "北京服务中心",
	//		Disease: "乳房胀痛",
	//	},
	//	{
	//		Name:    "内蒙服务中心",
	//		Disease: "胃寒",
	//	},
	//	{
	//		Name:    "杭州服务中心",
	//		Disease: "腰痛",
	//	},
	//}
	return
}

// GetCompletePlanUserRank // 获取服务站完成方案用户排行
// Author: [g-qs](https://gitee.com/g-qs)
func (userService *UserService) GetCompletePlanUserRank() (list []systemRes.StatisticalResults, err error) {
	endOfDay := time.Now().Format("2006-01-02")
	startOfDay := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
	if err = global.GVA_DB.Model(&catering.Food{}).
		Select("sys_users.name, COUNT(*) AS value, GROUP_CONCAT(fd_user.id) AS IDs").
		Joins("JOIN fd_user ON `fd_food`.user_id = fd_user.id").
		Joins("JOIN sys_users ON fd_user.service_center_id = sys_users.id").
		Where("`fd_food`.created_at >= ? AND `fd_food`.created_at < ?", startOfDay, endOfDay).
		Where("`fd_food`.breakfast IS NOT NULL AND `fd_food`.lunch IS NOT NULL AND `fd_food`.dinner IS NOT NULL AND `fd_food`.brain IS NOT NULL AND `fd_food`.sport IS NOT NULL").
		Group("sys_users.name").Order("value DESC").
		Scan(&list).Error; err != nil {
		return list, nil
	}
	return
}

// GetStationsRankByCpuNum // 获取服务站排名根据完成方案用户数量【服务中心】
// Author: [g-qs](https://gitee.com/g-qs)
func (userService *UserService) GetStationsRankByCpuNum(CentreID uint) (list []systemRes.StatisticalResults, err error) {
	endOfDay := time.Now().Format("2006-01-02")
	startOfDay := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
	if err = global.GVA_DB.Model(&catering.Food{}).
		Select("sys_users.name, COUNT(*) AS value, GROUP_CONCAT(fd_user.id) AS IDs").
		Joins("JOIN fd_user ON `fd_food`.user_id = fd_user.id").
		Joins("JOIN sys_users ON fd_user.service_center_id = sys_users.id").
		Where("`fd_food`.created_at BETWEEN ? AND ?", startOfDay, endOfDay).
		Where("`sys_users`.creator = ?", CentreID).
		Where("`fd_food`.breakfast IS NOT NULL AND `fd_food`.lunch IS NOT NULL AND `fd_food`.dinner IS NOT NULL AND `fd_food`.brain IS NOT NULL AND `fd_food`.sport IS NOT NULL").
		Group("sys_users.name").Order("value DESC").
		Scan(&list).Error; err != nil {
		return list, nil
	}
	return
}

// GetStationHealthManagerRank // 获取服务站健管师数量排名
// Author: [g-qs](https://gitee.com/g-qs)
func (userService *UserService) GetStationHealthManagerRank() (list []systemRes.StatisticalResults, err error) {
	// 查询每个服务中心下的健管中心数量
	err = global.GVA_DB.Model(&system.SysUser{}).
		Select("`sys_users`.name, COUNT(c.hm_id) AS value, GROUP_CONCAT(c.hm_id) AS IDs").
		Joins("LEFT JOIN fd_health_manager AS c ON c.store_id = `sys_users`.id").
		Where("`sys_users`.authority_id = 444").
		Group("`sys_users`.id").
		Order("value DESC").
		Scan(&list).Error
	return
}

// GetStationRankByUserNum 获取服务站排名根据用户数量【服务中心】
// Author: [g-qs](https://gitee.com/g-qs)
func (userService *UserService) GetStationRankByUserNum(CentreID uint) (list []systemRes.StatisticalResults, err error) {
	err = global.GVA_DB.Model(&system.SysUser{}).
		Select("`sys_users`.name, COUNT(c.id) AS value, GROUP_CONCAT(c.id) AS IDs").
		Joins("LEFT JOIN fd_user AS c ON c.service_center_id = `sys_users`.id").
		Where("`sys_users`.authority_id = 444 AND `sys_users`.creator = ?", CentreID).
		Group("`sys_users`.id").
		Order("value DESC").
		Scan(&list).Error
	return
}

// GetStationUserRank // 获取服务站用户数量排名
// Author: [g-qs](https://gitee.com/g-qs)
func (userService *UserService) GetStationUserRank() (list []systemRes.StatisticalResults, err error) {
	err = global.GVA_DB.Model(&system.SysUser{}).
		Select("`sys_users`.name, COUNT(c.id) AS value, GROUP_CONCAT(c.id) AS IDs").
		Joins("LEFT JOIN fd_user AS c ON c.service_center_id = `sys_users`.id").
		Where("`sys_users`.authority_id = 444").
		Group("`sys_users`.id").
		Order("value DESC").
		Scan(&list).Error
	//list = []systemRes.StatisticalResults{
	//	{
	//		Name:  "长春福墩健管服务站",
	//		Value: 380,
	//		IDs:   "13,28,29,7776,7777,7778,7781,7782,7790,7815,7858,7879,7922,7934,7941,7970,7989,8021,8041,8043,8055,8070,8093,8113,8126,8127,8128,8129,8130,8131,8133,8134,8135,8137,8138,8139,8140,8141,8142,8143,8144,8145,8146,8147,8148,8149,8150,8152,8155,8158,8160,8161,8164,8165,8166,8167,8171,8172,8173,8174,8175,8176,8177,8178,8179,8180,8181,8182,8183,8184,8185,8186,8187,8188,8191,8192,8193,8195,8197,8198,8199,8200,8201,8202,8206,8209,8210,8211,8213,8214,8215,8216,8219,8220,8221,8222,8223,8224,8225,8226,8227,8231,8233,8234,8236,8237,8238,8242,8243,8244,8245,8246,8247,8248,8249,8250,8252,8253,8254,8255,8256,8257,8261,8262,8265,8266,8267,8269,8270,8273,8274,8275,8276,8277,8279,8280,8281,8282,8283,8284,8286,8287,8289,8291,8294,8295,8299,8301,8303,8304,8307,8308,8309,8312,8313,8314,8315,8319,8320,8325,8333,8339,8340,8342,8343,8344,8345,8347,8349,8350,8351,8352,8359,8360,8361,8362,8363,8364,8365,8366,8368,8372,8373,8376,8377,8378,8380,8383,8384,8385,8386,8387,8389,8392,8399,8401,8402,8404,8406,8412,8413,8414,8415,8416,8418,8419,8420,8421,8423,8425,8428,8431,8436,8440,8441,8442,8446,8449,8463,8465,8466,8468,8475,8476,8481,8483,8489,8510,8527,8536,8542,8555,8589,8591,8595,8607,8608,8612,8614,8643,8663,8687,8689,8692,8704,8711,8712,8713,8719,8726,8729,8737,8739,8742,8747,8756,8761,8770,8772,8791,8799,8805,8813,8829,8830,8833,8835,8839,8843,8844,8846,8847,8849,8854,8855,8857,8864,8866,8867,8877,8878,8885,8889,8890,8897,8898,8899,8902,8910,8911,8917,8919,8920,8922,8925,8928,8931,8946,8948,8949,8950,8955,8956,8958,8962,8967,8968,8969,8970,8972,8975,8976,8978,8980,8981,8982,8983,8985,8986,8987,8988,8989,8991,8993,8994,8995,8996,9003,9008,9009,9014,9015,9017,9024,9025,9029,9031,9033,9038,9039,9041,9043,9044,9046,9051,9056,9057,9059,9064,9071,9075,9079,9081,9084,9085,9087,9093,9094,9096,9098,9099,9100,9101,9103,9105,9106,9107,9110,9112,9113,9114,9115,9117,9119,9120,9124,9125,9126,9127,9128",
	//	},
	//	{
	//		Name:  "南京市福墩服务站",
	//		Value: 303,
	//		IDs:   "7780,7993,7994,7995,7996,7997,7998,7999,8000,8001,8002,8003,8005,8006,8007,8008,8009,8010,8011,8012,8013,8014,8015,8016,8017,8018,8019,8020,8022,8023,8024,8025,8026,8027,8028,8029,8030,8031,8032,8033,8034,8035,8036,8037,8038,8039,8040,8042,8044,8045,8046,8047,8048,8049,8050,8051,8052,8053,8057,8058,8059,8060,8061,8062,8063,8065,8066,8067,8068,8069,8071,8072,8073,8074,8075,8076,8078,8079,8080,8081,8082,8083,8084,8085,8086,8087,8088,8089,8090,8091,8092,8094,8095,8097,8098,8099,8100,8101,8102,8103,8104,8105,8106,8107,8108,8109,8110,8111,8114,8115,8116,8119,8120,8121,8125,8136,8153,8154,8156,8157,8162,8163,8168,8169,8194,8196,8204,8207,8235,8239,8240,8241,8251,8258,8260,8271,8288,8290,8292,8302,8310,8311,8318,8322,8323,8324,8327,8328,8331,8336,8370,8375,8379,8381,8382,8390,8394,8398,8405,8407,8411,8426,8443,8451,8456,8467,8470,8472,8486,8490,8493,8498,8501,8504,8512,8535,8549,8575,8601,8611,8619,8646,8652,8653,8662,8664,8675,8678,8725,8731,8734,8735,8746,8758,8762,8773,8774,8777,8780,8782,8794,8798,8806,8807,8834,8840,8868,8869,8870,8871,8873,8874,8876,8879,8880,8881,8882,8884,8886,8887,8888,8891,8893,8894,8895,8896,8900,8901,8903,8904,8905,8906,8907,8908,8913,8914,8915,8916,8918,8921,8923,8924,8932,8934,8935,8937,8938,8942,8954,8957,8959,8960,8963,8964,8973,8974,8977,8979,8984,8998,8999,9002,9007,9016,9019,9022,9026,9027,9030,9034,9036,9037,9040,9042,9045,9047,9049,9050,9053,9055,9060,9067,9070,9072,9073,9076,9078,9080,9082,9083,9086,9088,9089,9090,9095,9097,9102,9104,9116,9118,9121,9122,9123",
	//	},
	//	{
	//		Name:  "青岛市市北区福墩服务站",
	//		Value: 177,
	//		IDs:   "7793,7800,7810,7825,7827,7842,7845,7854,7861,7863,7864,7865,7866,7867,7868,7869,7871,7873,7874,7877,7881,7882,7883,7884,7885,7886,7887,7888,7889,7890,7891,7892,7893,7894,7895,7896,7897,7898,7899,7900,7901,7902,7903,7904,7905,7906,7907,7908,7909,7910,7911,7912,7913,7914,7915,7916,7917,7919,7926,7927,7928,7930,7931,7932,7933,7938,7939,7940,7944,7945,7946,7947,7948,7950,7951,7954,7956,7957,7958,7959,7960,7961,7965,7967,7969,7973,7974,7975,7976,7977,7979,7981,7982,7983,7984,7985,7986,7987,7988,7990,7991,8004,8064,8077,8112,8117,8118,8124,8132,8170,8189,8203,8217,8218,8228,8230,8232,8259,8264,8278,8285,8300,8306,8316,8332,8334,8353,8354,8356,8357,8358,8371,8388,8393,8397,8424,8432,8433,8437,8477,8525,8576,8642,8647,8661,8667,8684,8685,8706,8710,8722,8728,8792,8793,8804,8809,8810,8812,8821,8824,8875,8927,8990,8992,8997,9004,9006,9010,9011,9012,9028,9032,9054,9077,9091,9092,9111",
	//	},
	//	{
	//		Name:  "绍兴市诸暨市福墩服务站",
	//		Value: 153,
	//		IDs:   "7925,7935,7936,7949,7952,7953,7955,8293,8305,8335,8337,8338,8341,8367,8417,8427,8429,8430,8434,8471,8478,8480,8484,8485,8487,8495,8500,8503,8505,8506,8507,8508,8509,8511,8513,8514,8515,8516,8517,8518,8519,8520,8521,8522,8523,8524,8530,8531,8537,8538,8539,8540,8546,8547,8548,8550,8551,8552,8554,8556,8557,8558,8559,8560,8561,8562,8563,8564,8565,8566,8567,8568,8569,8570,8571,8572,8573,8574,8578,8579,8580,8581,8583,8584,8585,8586,8587,8588,8592,8593,8596,8597,8598,8600,8602,8603,8604,8609,8610,8613,8615,8616,8617,8620,8621,8622,8623,8624,8626,8627,8628,8629,8630,8631,8634,8635,8636,8637,8640,8641,8644,8649,8650,8651,8654,8655,8658,8659,8660,8665,8668,8693,8694,8697,8698,8699,8700,8708,8714,8715,8717,8718,8738,8748,8765,8766,8787,8788,8811,8817,8845,8848,8852",
	//	},
	//	{
	//		Name:  "重庆市永川区福墩服务站",
	//		Value: 78,
	//		IDs:   "7795,7796,7798,7799,7801,7804,7806,7807,7814,7816,7820,7822,7823,7828,7829,7830,7831,7832,7833,7834,7837,7838,7839,7840,7841,7843,7844,7862,7870,7876,7880,7920,7921,7923,7924,7937,7964,7971,7972,7980,8205,8212,8263,8268,8391,8403,8410,8454,8459,8460,8461,8469,8496,8502,8528,8529,8532,8533,8590,8632,8638,8673,8827,8892,8929,8939,8940,8941,8943,8944,8945,8947,8951,8952,8953,8966,9108,9109",
	//	},
	//	{
	//		Name:  "北京福墩健管服务站",
	//		Value: 63,
	//		IDs:   "7836,8447,8452,8464,8491,8666,8670,8683,8690,8695,8701,8705,8707,8779,8784,8785,8786,8802,8803,8814,8815,8818,8819,8820,8822,8823,8825,8826,8828,8836,8837,8838,8842,8850,8851,8853,8856,8858,8860,8861,8862,8863,8865,8909,8926,8930,8936,9000,9001,9005,9013,9018,9023,9035,9052,9058,9061,9062,9063,9065,9066,9068,9074",
	//	},
	//	{
	//		Name:  "杭州市西湖区福墩服务站",
	//		Value: 61,
	//		IDs:   "8473,8474,8479,8499,8526,8541,8544,8545,8605,8606,8639,8656,8671,8672,8677,8679,8680,8681,8682,8688,8702,8703,8716,8720,8721,8724,8727,8730,8736,8740,8741,8745,8749,8750,8751,8752,8753,8754,8757,8760,8763,8768,8771,8775,8776,8778,8781,8783,8789,8790,8795,8796,8797,8808,8831,8832,8841,8859,8912,9048,9069",
	//	},
	//	{
	//		Name:  "驻马店市上蔡县福墩服务站",
	//		Value: 32,
	//		IDs:   "7786,7787,7789,7808,7813,7817,7818,7824,7826,7850,7857,7878,7962,7963,7966,8296,8297,8298,8317,8330,8395,8396,8408,8422,8455,8618,8645,8676,8686,8691,9020,9021",
	//	},
	//	{
	//		Name:  "海口福墩健管服务站",
	//		Value: 31,
	//		IDs:   "7779,7802,7819,7846,7872,8159,8208,8439,8444,8445,8448,8457,8462,8494,8534,8543,8577,8599,8674,8709,8723,8743,8744,8755,8759,8764,8769,8800,8883,8965,8971",
	//	},
	//	{
	//		Name:  "淄博市张店区福墩服务站",
	//		Value: 13,
	//		IDs:   "7788,7835,7847,7848,7849,7851,7852,7853,7855,7856,7859,7860,8346",
	//	},
	//	{
	//		Name:  "佳木斯市富锦市福墩服务站",
	//		Value: 12,
	//		IDs:   "7791,7811,7875,8054,8056,8096,8122,8123,8229,8272,8374,8633",
	//	},
	//	{
	//		Name:  "淄博市周村区福墩服务站",
	//		Value: 10,
	//		IDs:   "7809,7918,7929,7968,7978,8151,8450,8453,8458,8482",
	//	},
	//	{
	//		Name:  "长沙福墩健管服务站",
	//		Value: 9,
	//		IDs:   "7821,7992,8190,8355,8488,8492,8582,8696,8767",
	//	},
	//	{
	//		Name:  "天津市滨海新区福墩服务站",
	//		Value: 9,
	//		IDs:   "7805,7812,8369,8400,8497,8625,8657,8933,8961",
	//	},
	//	{
	//		Name:  "赤峰市敖汉旗福墩服务站",
	//		Value: 7,
	//		IDs:   "7797,7942,7943,8321,8409,8435,8872",
	//	},
	//	{
	//		Name:  "青岛城阳福墩健管服务站",
	//		Value: 5,
	//		IDs:   "7792,7794,7803,8732,8733",
	//	},
	//	{
	//		Name:  "上海市徐汇区福墩服务站",
	//		Value: 4,
	//		IDs:   "2,3,11,33",
	//	},
	//	{
	//		Name:  "诸暨市东兴社区福墩服务站",
	//		Value: 4,
	//		IDs:   "8348,8553,8594,8648",
	//	},
	//	{
	//		Name:  "长春福墩健管服务站2",
	//		Value: 2,
	//		IDs:   "1,12",
	//	},
	//	{
	//		Name:  "诸暨市东盛社区福墩服务站",
	//		Value: 2,
	//		IDs:   "8326,8669",
	//	},
	//	{
	//		Name:  "诸暨市北社区福墩服务站",
	//		Value: 2,
	//		IDs:   "8801,8816",
	//	},
	//	{
	//		Name:  "贵州福墩健管服务站",
	//		Value: 1,
	//		IDs:   "8438",
	//	},
	//	{
	//		Name:  "诸暨市江东社区福墩服务站",
	//		Value: 1,
	//		IDs:   "8329",
	//	},
	//	{
	//		Name:  "阜阳界首福墩健管服务站",
	//		Value: 0,
	//		IDs:   "",
	//	},
	//	{
	//		Name:  "哈尔滨福墩健管服务站",
	//		Value: 0,
	//		IDs:   "",
	//	},
	//	{
	//		Name:  "重庆永川福墩健管服务站2",
	//		Value: 0,
	//		IDs:   "",
	//	},
	//	{
	//		Name:  "诸暨市友谊社区福墩服务站",
	//		Value: 0,
	//		IDs:   "",
	//	},
	//	{
	//		Name:  "诸暨市福门社区福墩服务站",
	//		Value: 0,
	//		IDs:   "",
	//	},
	//	{
	//		Name:  "杭州市滨江区银杏汇社区福墩服务站",
	//		Value: 0,
	//		IDs:   "",
	//	},
	//	{
	//		Name:  "成都福墩健管服务站",
	//		Value: 0,
	//		IDs:   "",
	//	},
	//	{
	//		Name:  "温州福墩健管服务站",
	//		Value: 0,
	//		IDs:   "",
	//	},
	//	{
	//		Name:  "桂林福墩健管服务站",
	//		Value: 0,
	//		IDs:   "",
	//	},
	//	{
	//		Name:  "南京市秦淮区福墩服务站",
	//		Value: 0,
	//		IDs:   "",
	//	},
	//	{
	//		Name:  "吉林市船营区福墩服务站",
	//		Value: 0,
	//		IDs:   "",
	//	},
	//	{
	//		Name:  "微生活",
	//		Value: 0,
	//		IDs:   "",
	//	},
	//	{
	//		Name:  "诸暨市凤凰社区福墩服务站",
	//		Value: 0,
	//		IDs:   "",
	//	},
	//}
	return
}

// GetStationDiseaseList // 获取服务站主病症列表
// Author: [g-qs](https://gitee.com/g-qs)
func (userService *UserService) GetStationDiseaseList() (list []systemRes.MainDiseaseList, err error) {
	// 1. 获取所有服务站
	var stations []system.SysUser
	if err := global.GVA_DB.Where("authority_id = ?", 444).Find(&stations).Error; err != nil {
		return nil, err
	}
	if len(stations) == 0 {
		return []systemRes.MainDiseaseList{}, nil
	}

	// 收集服务站ID
	stationIDs := make([]uint, 0, len(stations))
	for _, station := range stations {
		stationIDs = append(stationIDs, station.ID)
	}

	// 2. 批量获取所有服务站的用户
	var users []user.User
	if err := global.GVA_DB.Where("service_center_id IN ?", stationIDs).Find(&users).Error; err != nil {
		return nil, err
	}
	if len(users) == 0 {
		return []systemRes.MainDiseaseList{}, nil
	}

	// 构建用户ID到服务站的映射和用户ID列表
	userIDToStationID := make(map[uint]uint, len(users))
	userIDs := make([]uint, 0, len(users))
	for _, user := range users {
		userIDToStationID[user.ID] = user.ServiceCenterID
		userIDs = append(userIDs, user.ID)
	}

	// 3. 批量获取所有用户的最后一条报告记录（优化查询）
	var records []disease.DiseaseReportRecord
	subQuery := global.GVA_DB.Model(&disease.DiseaseReportRecord{}).
		Select("uid, MAX(created_at) as latest_date").
		Where("uid IN ?", userIDs).
		Group("uid")

	if err := global.GVA_DB.Table("fd_disease_report_record as r").
		Joins("INNER JOIN (?) AS l ON r.uid = l.uid AND r.created_at = l.latest_date", subQuery).
		Find(&records).Error; err != nil {
		return nil, err
	}

	// 4. 统计病症出现次数（内存计算）
	// stationID -> (diseaseID -> count)
	diseaseStats := make(map[uint]map[string]int)
	for _, record := range records {
		stationID := userIDToStationID[record.Uid]
		if stationID == 0 {
			continue
		}

		if _, ok := diseaseStats[stationID]; !ok {
			diseaseStats[stationID] = make(map[string]int)
		}

		for _, diseaseID := range strings.Split(record.BzIDs, ",") {
			if diseaseID == "" {
				continue
			}
			diseaseStats[stationID][diseaseID]++
		}
	}

	// 5. 收集所有需要查询的病症ID
	var diseaseIDs []string
	for _, stats := range diseaseStats {
		for diseaseID := range stats {
			diseaseIDs = append(diseaseIDs, diseaseID)
		}
	}

	// 6. 批量获取病症名称
	var diseases []disease.Disease
	if err = global.GVA_DB.Where("id IN ?", diseaseIDs).Find(&diseases).Error; err != nil {
		return nil, err
	}

	diseaseNameMap := make(map[string]string, len(diseases))
	for _, d := range diseases {
		diseaseNameMap[strconv.Itoa(int(d.ID))] = d.Name
	}

	// 7. 构建最终结果
	list = make([]systemRes.MainDiseaseList, 0, len(stations))
	for _, station := range stations {
		stats, ok := diseaseStats[station.ID]
		if !ok || len(stats) == 0 {
			continue
		}

		// 找出最大出现次数的病症
		maxCount := 0
		var mainDiseaseID string
		for diseaseID, count := range stats {
			if count > maxCount || (count == maxCount && diseaseID < mainDiseaseID) {
				maxCount = count
				mainDiseaseID = diseaseID
			}
		}

		if mainDiseaseID != "" {
			list = append(list, systemRes.MainDiseaseList{
				Name:    station.Name,
				Disease: diseaseNameMap[mainDiseaseID],
			})
		}
	}
	//list = []systemRes.MainDiseaseList{
	//	{
	//		Name:    "海口福墩健管服务站",
	//		Disease: "胃寒",
	//	},
	//	{
	//		Name:    "长沙福墩健管服务站",
	//		Disease: "白带过多",
	//	},
	//	{
	//		Name:    "上海市徐汇区福墩服务站",
	//		Disease: "腰膝冷痛",
	//	},
	//	{
	//		Name:    "青岛市市北区福墩服务站",
	//		Disease: "眼花",
	//	},
	//	{
	//		Name:    "长春福墩健管服务站2",
	//		Disease: "偏头痛",
	//	},
	//	{
	//		Name:    "长春福墩健管服务站",
	//		Disease: "腰膝酸软",
	//	},
	//	{
	//		Name:    "南京市福墩服务站",
	//		Disease: "视力减退",
	//	},
	//	{
	//		Name:    "重庆市永川区福墩服务站",
	//		Disease: "健忘",
	//	},
	//	{
	//		Name:    "绍兴市诸暨市福墩服务站",
	//		Disease: "眼花",
	//	},
	//	{
	//		Name:    "驻马店市上蔡县福墩服务站",
	//		Disease: "尿黄",
	//	},
	//	{
	//		Name:    "淄博市周村区福墩服务站",
	//		Disease: "高血压",
	//	},
	//	{
	//		Name:    "天津市滨海新区福墩服务站",
	//		Disease: "便秘",
	//	},
	//	{
	//		Name:    "北京福墩健管服务站",
	//		Disease: "乳房胀痛",
	//	},
	//	{
	//		Name:    "赤峰市敖汉旗福墩服务站",
	//		Disease: "胃寒",
	//	},
	//	{
	//		Name:    "诸暨市东盛社区福墩服务站",
	//		Disease: "打鼾",
	//	},
	//	{
	//		Name:    "诸暨市东兴社区福墩服务站",
	//		Disease: "偏头痛",
	//	},
	//	{
	//		Name:    "杭州市西湖区福墩服务站",
	//		Disease: "乳房胀痛",
	//	},
	//}
	return
}

// GetCentreDiseaseList 获取服务中心主病症列表【服务中心】
// Author: [g-qs](https://gitee.com/g-qs)
func (userService *UserService) GetCentreDiseaseList(CentreID uint) (list []systemRes.MainDiseaseList, err error) {
	var stations []system.SysUser
	var users []user.User
	var records []disease.DiseaseReportRecord
	// 查询所有的服务站
	if stationsErr := global.GVA_DB.Where("authority_id = 444 AND creator = ?", CentreID).Find(&stations).Error; stationsErr != nil {
		return list, stationsErr
	}
	for _, stationValue := range stations {
		// 查询与当前服务站关联的所有用户
		global.GVA_DB.Table("fd_user USE INDEX (service_center_id)").Where("service_center_id = ?", stationValue.ID).Find(&users)
		var userIDs []uint
		for _, userValue := range users {
			userIDs = append(userIDs, userValue.ID)
		}
		// 查询与当前用户关联的所有病症报告记录
		global.GVA_DB.Table("fd_disease_report_record USE INDEX (uid)").
			Joins("INNER JOIN ( SELECT uid, MAX( created_at ) AS latest_date FROM fd_disease_report_record WHERE uid IN (?) GROUP BY uid ) latest_records ON fd_disease_report_record.uid = latest_records.uid", userIDs).
			Where("fd_disease_report_record.created_at = latest_records.latest_date").Find(&records)
		// 统计疾病出现次数
		diseaseCounts := map[string]int{}
		for _, record := range records {
			diseases := strings.Split(record.BzIDs, ",")
			for _, diseaseID := range diseases {
				diseaseCounts[diseaseID]++
			}
		}
		// 找出出现次数最多的疾病
		maxCount := 0
		mainDiseaseID := ""
		for diseaseID, count := range diseaseCounts {
			if count > maxCount {
				maxCount = count
				mainDiseaseID = diseaseID
			}
		}
		// 查询主要疾病名称
		var bz disease.Disease
		if bzErr := global.GVA_DB.First(&bz, "id = ?", mainDiseaseID).Error; bzErr != nil {
			continue
		}
		list = append(list, systemRes.MainDiseaseList{
			Name:    stationValue.Name,
			Disease: bz.Name,
		})
	}
	return
}

// GetServiceStationListByIDs 根据ID集合获取用户列表【超管】
func (userService *UserService) GetServiceStationListByIDs(info systemReq.ServiceStation) (list []systemRes.ServiceStations, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&system.SysUser{})
	if info.UIDs != "" {
		IDsStr := strings.Trim(info.UIDs, "[]")
		IDsArr := strings.Split(IDsStr, ",")
		db = db.Where("sys_users.id IN (?)", IDsArr)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}
	err = db.Select("sys_users.`id`, sys_users.`nick_name`, sys_users.`phone`, sys_users.`name`, b.`name` AS serviceCenter, " +
		"(SELECT name FROM geo_provinces WHERE code = sys_users.province) AS province, " +
		"(SELECT name FROM geo_cities WHERE code = sys_users.city) AS city, " +
		"(SELECT name FROM geo_areas WHERE code = sys_users.area) AS area").
		Joins("LEFT JOIN sys_users AS b ON sys_users.`creator` = b.id").
		Order("sys_users.id ASC").
		Scan(&list).Error
	return
}

// GetServiceCenterList 分页获取服务中心列表
// Author [g-qs](https://gitee.com/g-qs)
func (userService *UserService) GetServiceCenterList(info systemReq.BranchSearch) (list interface{}, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	db := global.GVA_DB.Model(&system.SysUser{}).Where("authority_id = ?", 333)
	var userList []system.SysUser
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.Name != "" {
		db = db.Where("name LIKE ?", "%"+info.Name+"%")
	}
	if info.NickName != "" {
		db = db.Where("nick_name LIKE ?", "%"+info.NickName+"%")
	}
	if info.Phone != "" {
		db = db.Where("phone LIKE ?", "%"+info.Phone+"%")
	}
	if info.Province != "" {
		db = db.Where("province = ?", info.Province)
	}
	if info.City != "" {
		db = db.Where("city = ?", info.City)
	}
	if info.Area != "" {
		db = db.Where("area = ?", info.Area)
	}
	if info.Addr != "" {
		db = db.Where("addr LIKE ?", "%"+info.Addr+"%")
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Limit(limit).Offset(offset).Preload("Authorities").Preload("Authority").Preload("CreatorInfo").Find(&userList).Error
	return userList, total, err
}

// GetSysUserInfo 根据id获取用户详情
// Author [g-qs](https://github.com/g-qs)
func (userService *UserService) GetSysUserInfo(id uint) (info system.SysUser, err error) {
	err = global.GVA_DB.Where("id = ?", id).Preload("Authorities").Preload("Authority").Preload("CreatorInfo").First(&info).Error
	return
}

// GetHealthManageCenterList 分页获取健管中心数据
// Author [g-qs](https://gitee.com/g-qs)
func (userService *UserService) GetHealthManageCenterList(info systemReq.BranchSearch) (list interface{}, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	db := global.GVA_DB.Model(&system.SysUser{}).Where("authority_id = ? AND creator = ?", 444, info.Creator)
	var userList []system.SysUser
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.Name != "" {
		db = db.Where("name LIKE ?", "%"+info.Name+"%")
	}
	if info.NickName != "" {
		db = db.Where("nick_name LIKE ?", "%"+info.NickName+"%")
	}
	if info.Phone != "" {
		db = db.Where("phone LIKE ?", "%"+info.Phone+"%")
	}
	if info.Province != "" {
		db = db.Where("province = ?", info.Province)
	}
	if info.City != "" {
		db = db.Where("city = ?", info.City)
	}
	if info.Area != "" {
		db = db.Where("area = ?", info.Area)
	}
	if info.Addr != "" {
		db = db.Where("addr LIKE ?", "%"+info.Addr+"%")
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Limit(limit).Offset(offset).Preload("Authorities").Preload("Authority").Preload("CreatorInfo").Find(&userList).Error
	return userList, total, err
}

// GetAllServiceStationList 分页获取全部服务站列表【超管】
// Author [g-qs](https://gitee.com/g-qs)
func (userService *UserService) GetAllServiceStationList(info systemReq.BranchSearch) (list interface{}, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	db := global.GVA_DB.Model(&system.SysUser{}).Where("authority_id = 444")
	var userList []system.SysUser
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.Name != "" {
		db = db.Where("name LIKE ?", "%"+info.Name+"%")
	}
	if info.NickName != "" {
		db = db.Where("nick_name LIKE ?", "%"+info.NickName+"%")
	}
	if info.Phone != "" {
		db = db.Where("phone LIKE ?", "%"+info.Phone+"%")
	}
	if info.Province != "" {
		db = db.Where("province = ?", info.Province)
	}
	if info.City != "" {
		db = db.Where("city = ?", info.City)
	}
	if info.Area != "" {
		db = db.Where("area = ?", info.Area)
	}
	if info.Addr != "" {
		db = db.Where("addr LIKE ?", "%"+info.Addr+"%")
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Limit(limit).Offset(offset).Preload("Authorities").Preload("Authority").Preload("CreatorInfo").Find(&userList).Error
	return userList, total, err
}

func getSysUserCityIDs(users []system.SysUser) []string {
	ids := make([]string, len(users))
	for i, g := range users {
		ids[i] = g.City
	}
	return ids
}

func getSysUserProvinceIDs(users []system.SysUser) []string {
	ids := make([]string, len(users))
	for i, g := range users {
		ids[i] = g.Province
	}
	return ids
}
